package com.jiamao.goplay.ui.discover.vm;

import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.jiamao.goplay.BuildConfig;
import com.jiamao.goplay.bean.GoToWhereNumberItem;
import com.jiamao.goplay.config.Config;
import com.jiamao.goplay.ui.discover.SearchFragment;
import com.jiamao.goplay.ui.user.OthersUserInfoFragment;
import com.jiamao.goplay.ui.user.vm.UserAddrDetailVM;
import com.netease.nim.uikit.api.NimUIKit;

import java.security.SecurityPermission;

import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.SPUtils;

public class AddrDetailItemVM extends MultiItemViewModel {

    public GoToWhereNumberItem data;

    public ObservableField<String> imgUrl = new ObservableField<>("");
    public ObservableField<String> name = new ObservableField<>("");
    public ObservableField<String> statusDes = new ObservableField<>("");
    public ObservableField<String> userDes = new ObservableField<>("");
    public ObservableField<String> yqStr = new ObservableField<>("邀请他去");
    public ObservableInt ypdBtnVisibility = new ObservableInt(View.GONE);
    public ObservableInt yqBtnVisibility = new ObservableInt(View.GONE);
    public ObservableInt yqnBtnVisibility = new ObservableInt(View.GONE);
    public ObservableInt jsyqBtnVisibility = new ObservableInt(View.GONE);
    public ObservableInt yyqBtnVisibility = new ObservableInt(View.GONE);

    public ObservableField<Integer> isVip = new ObservableField<>();
    public ObservableField<Integer> isShming = new ObservableField<>();
    public ObservableField<Integer> isRenXiang = new ObservableField<>();



    private String id,uid;
   // public ObservableField<String> University = new ObservableField<>("");
    //private GoToWhereNumberItem data;
    public AddrDetailItemVM(@NonNull BaseViewModel viewModel, GoToWhereNumberItem item) {
        super(viewModel);
        this.data  = item;
        if (item==null){
            return;
        }

        yqStr.set("邀请"+(item.getGender()==1?"他":"她")+"去");
        id = item.getInvID();
        uid = item.getUid();
        imgUrl.set(item.getAvatar());
        name.set(item.getNickname());

        StringBuffer addr = new StringBuffer();

        if (!TextUtils.isEmpty(item.getDistance())){
            addr.append(item.getDistance()+"km");
            if (!TextUtils.isEmpty(item.getCity())){
                addr.append("，");
                addr.append(item.getCity());
            }
            if (!TextUtils.isEmpty(item.getArea())){
                addr.append("，");
                addr.append(item.getArea());
            }
        }else if(!TextUtils.isEmpty(item.getCity())){
            addr.append(item.getCity());
            if (!TextUtils.isEmpty(item.getArea())){
                addr.append("，");
                addr.append(item.getArea());
            }
        }

        userDes.set(addr.toString());

        String uid = SPUtils.getInstance().getString(Config.UID);



//        if (!TextUtils.isEmpty(item.getBaseInfo().getIsActive()) && (item.getBaseInfo().getIsActive().equals("现在活跃") || item.getBaseInfo().getIsActive().equals("当前活跃"))){
//            zy.set(item.getBaseInfo().getIsActive());
//            zyBtnVisibility.set(View.VISIBLE);
//        }
         if (!TextUtils.isEmpty( item.getHeight()) && !item.getHeight().equals("0")){
             KLog.d("aaaaaaa");

            if (!TextUtils.isEmpty( item.getWeight())){
                statusDes.set(item.getHeight()+"/"+item.getWeight());
            }else{
                statusDes.set(item.getHeight());
            }
            // zy.set(item.getUserInfo().getHeight()+"/"+item.getUserInfo().getWeight());

        }
        else if (!TextUtils.isEmpty( item.getWeight()) ){

             statusDes.set(item.getWeight());

        }

        else if(!TextUtils.isEmpty(item.getIndustry()) ){

            if (!TextUtils.isEmpty( item.getPosition())){
                statusDes.set(item.getIndustry()+"/"+item.getPosition());
            }else{
                statusDes.set(item.getIndustry());
            }

            //  zy.set(item.getUserInfo().getIndustry()+"/"+item.getUserInfo().getPosition());
            //zyBtnVisibility.set(View.VISIBLE);
        }
         else if(!TextUtils.isEmpty(item.getPosition()) ){
                 statusDes.set(item.getPosition());
             //  zy.set(item.getUserInfo().getIndustry()+"/"+item.getUserInfo().getPosition());
             //zyBtnVisibility.set(View.VISIBLE);
         }
        else if(!TextUtils.isEmpty(item.getUniversity())){
             statusDes.set(item.getUniversity());
             KLog.d("aaaaaaa");

        }else{
            KLog.d("aaaaaaa");
            statusDes.set("还没添加更多的资料…");
         }
//        else if(!TextUtils.isEmpty(item.getCompany())  ){
//             statusDes.set(item.getCompany());
//            //zyBtnVisibility.set(View.VISIBLE);
//        }
//        else if(!TextUtils.isEmpty(item.getInterest()) ){
//             statusDes.set(item.getInterest());
//          //  zyBtnVisibility.set(View.VISIBLE);
//        }else {
//           // zyBtnVisibility.set(View.GONE);
//        }



        KLog.d("uid:"+uid+"   ta uid:"+item.getUid());
        if (!uid.equals(item.getUid())){

            //可以邀请
            if (item.getStatus() == 0 ){

                if (item.getGender()==1){
                    yqnBtnVisibility.set(View.VISIBLE);
                }else {
                    yqBtnVisibility.set(View.VISIBLE);
                }

//                //  yqBtnVisibility.set(View.VISIBLE);
//                StringBuffer strDes = new StringBuffer();
//
//                if (!TextUtils.isEmpty(item.getIndustry())){
//                    strDes.append(item.getIndustry());
//                    if (!TextUtils.isEmpty(item.getPosition())){
//                        strDes.append("/");
//                        strDes.append(item.getPosition());
//                    }
//                }else if(!TextUtils.isEmpty(item.getPosition())){
//                    strDes.append(item.getPosition());
//                }
//                statusDes.set(strDes.toString());
            }else if(item.getStatus() ==1){
                //接受邀请
                jsyqBtnVisibility.set(View.VISIBLE);
                statusDes.set(item.getStatusDes());

            }else if(item.getStatus() ==2)  {
                //已经配对
                ypdBtnVisibility.set(View.VISIBLE);
                statusDes.set(item.getStatusDes());
            }else if(item.getStatus() ==3){
                yyqBtnVisibility.set(View.VISIBLE);
                statusDes.set(item.getStatusDes());
            }
        }
        else {
            yqBtnVisibility.set(View.INVISIBLE);
        }



            isVip.set( View.GONE);
            isRenXiang.set(item.getAvatarVerified() == 0 ? View.GONE : View.VISIBLE);
            isShming.set(item.getRealnameVerified() == 0 ? View.GONE : View.VISIBLE);



    }

    //条目的点击事件
    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            viewModel.startContainerActivity(SearchFragment.class.getCanonicalName());
        }
    });

    //条目的点击事件
    public BindingCommand imgClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (data ==null){
                return;
            }
            Bundle bd = new Bundle();
            bd.putString("uid",data.getUid()+"");
            viewModel.startContainerActivity(OthersUserInfoFragment.class.getCanonicalName(),bd);
          //  viewModel.startContainerActivity(SearchFragment.class.getCanonicalName());
        }
    });



    //条目的点击事件
    public BindingCommand jsyqClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (viewModel instanceof UserAddrDetailVM){
                ((UserAddrDetailVM)viewModel).jieshouyaoqing(id,AddrDetailItemVM.this);
            }else {
                ((AddrDetailVM)viewModel).jieshouyaoqing(id,AddrDetailItemVM.this);
            }

        }
    });


    //条目的点击事件
    public BindingCommand yqClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (viewModel instanceof UserAddrDetailVM){
                ((UserAddrDetailVM)viewModel).yaoqing(uid,AddrDetailItemVM.this);
            }else {
                ((AddrDetailVM)viewModel).yaoqing(uid,AddrDetailItemVM.this);
            }

           // viewModel.startContainerActivity(SearchFragment.class.getCanonicalName());
        }
    });

    //条目的点击事件
    public BindingCommand ypdClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            if (viewModel instanceof UserAddrDetailVM) {
                ((UserAddrDetailVM)viewModel).startChat(uid);
            }else {
                ((AddrDetailVM)viewModel).startChat(uid);
            }

        //    NimUIKit.startP2PSession(viewModel.getApplication(), uid);
            //((AddrDetailVM)viewModel).yaoqing(uid);
            // viewModel.startContainerActivity(SearchFragment.class.getCanonicalName());
        }
    });



}
